﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FifteenPuzzleCon
{
    class FifteenPuzzle
    {

        public class position
        {
            public int X { get; set; }
            public int Y { get; set; }
        }


        public int[,] puzzleMatrix = new int[4, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 0 } };

        public void drawBoard()
        {
            Console.SetCursorPosition(0, 0);
            Console.WriteLine("┌──┬──┬──┬──┐");
            Console.WriteLine("│  │  │  │  │");
            Console.WriteLine("├──┼──┼──┼──┤");
            Console.WriteLine("│  │  │  │  │");
            Console.WriteLine("├──┼──┼──┼──┤");
            Console.WriteLine("│  │  │  │  │");
            Console.WriteLine("├──┼──┼──┼──┤");
            Console.WriteLine("│  │  │  │  │");
            Console.WriteLine("└──┴──┴──┴──┘");


            for (int i = 1; i < 8; i = i + 2)
            {
                for( int j = 1; j < 11 ; j = j+3 )
                {
                    Console.SetCursorPosition(j, i);
                    Console.Write(puzzleMatrix[(i-1)/2, j/3]);
                }
            }
        }


        public bool makeMove( position zero )
        {
            
            ConsoleKeyInfo keyInfo = Console.ReadKey();
            position other = new position();

            if (keyInfo.Key == ConsoleKey.DownArrow)
            {
                if (zero.Y < 3)
                {
                    other.X = zero.X;
                    other.Y = zero.Y + 1;
                    swap(zero, other);
                    return true;
                }
                else
                {
                    Console.SetCursorPosition(20, 20);
                    Console.WriteLine("Can't move that way");
                    return true;
                }
            }
            else if (keyInfo.Key == ConsoleKey.UpArrow)
            {
                if (zero.Y > 0)
                {
                    other.X = zero.X;
                    other.Y = zero.Y - 1;
                    swap(zero, other);
                    return true;
                }
                else
                {
                    Console.SetCursorPosition(20, 20);
                    Console.WriteLine("Can't move that way");
                    return true;
                }
            }
            else if (keyInfo.Key == ConsoleKey.LeftArrow)
            {
                if (zero.X > 0)
                {
                    other.X = zero.X - 1;
                    other.Y = zero.Y;
                    swap(zero, other);
                    return true;
                }
                else
                {
                    Console.SetCursorPosition(20, 20);
                    Console.WriteLine("Can't move that way");
                    return true;
                }
            }
            else if (keyInfo.Key == ConsoleKey.RightArrow)
            {
                if (zero.X < 3)
                {
                    other.X = zero.X + 1;
                    other.Y = zero.Y;
                    swap(zero, other);
                    return true;
                }
                else
                {
                    Console.SetCursorPosition(20, 20);
                    Console.WriteLine("Can't move that way");
                    return true;
                }
            }
            else if (keyInfo.Key == ConsoleKey.Escape)
            {
                return false;
            }
            else
                return true;


        }


        public void makeRandomMove(position zero, int randomNumber)
        {

            position other = new position();

            
            if (randomNumber == 1 ) //Down
            {
                if (zero.Y < 3)
                {
                    other.X = zero.X;
                    other.Y = zero.Y + 1;
                    swap(zero, other);
                }
            }
            else if (randomNumber == 2) //Up
            {
                if (zero.Y > 0)
                {
                    other.X = zero.X;
                    other.Y = zero.Y - 1;
                    swap(zero, other);
                }
            }
            else if (randomNumber == 3) //Left
            {
                if (zero.X > 0)
                {
                    other.X = zero.X - 1;
                    other.Y = zero.Y;
                    swap(zero, other);
                }
            }
            else if (randomNumber == 4) //Right
            {
                if (zero.X < 3)
                {
                    other.X = zero.X + 1;
                    other.Y = zero.Y;
                    swap(zero, other);
                }
            }
        }


        public void swap( position zero, position other )
        {
            int temp;

            temp = puzzleMatrix[zero.Y, zero.X];
            puzzleMatrix[zero.Y, zero.X] = puzzleMatrix[other.Y, other.X];
            puzzleMatrix[other.Y, other.X] = temp;

        }

        public position findFree()
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (puzzleMatrix[j, i] == 0)
                    {
                        position zero = new position();
                        zero.X = i;
                        zero.Y = j;

                        return zero;
                    }
                        
                }
            }
            return null;
        }

        public bool detectWin()
        {
            int i = 1;
            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    if (puzzleMatrix[row, col] != i)
                    {
                        return false;
                    }

                    i = (i + 1) % 16;
                }
            }
            return true;
        }

        
    }
}
